package com.deer.wms.base.system.web.ware;

import com.deer.wms.base.system.model.ware.AreaInfo;
import com.deer.wms.base.system.model.ware.AreaInfoDto;
import com.deer.wms.base.system.service.bill.IBillOutDetailService;
import com.deer.wms.base.system.service.ware.IAreaInfoService;
import com.deer.wms.common.annotation.Log;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.enums.BusinessType;
import com.deer.wms.common.utils.poi.ExcelUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货区设置 信息操作处理
 * 

 * @date 2019-05-08
 */
@Controller
@RequestMapping("/system/areaInfo")
public class AreaInfoController extends BaseController
{
    private String prefix = "system/areaInfo";
	
	@Autowired
	private IAreaInfoService areaInfoService;
	@Autowired
	private IBillOutDetailService billOutDetailService;
	@RequiresPermissions("system:areaInfo:view")
	@GetMapping()
	public String areaInfo()
	{
	    return prefix + "/areaInfo";
	}

	@RequiresPermissions("system:areaInfo:view")
	@GetMapping("/transitGodown")
	public String aareaInfo()
	{
		return prefix + "/transitGodown";
	}
	//二维码生成
	@RequestMapping("qr")
	public void QR(HttpServletRequest request, HttpServletResponse response, @RequestParam String ids) throws Exception {


		try {
                 /* WareInfo w=new WareInfo();
            w.setWareId(Integer.parseInt(id));*/


		} catch (Exception e){
			e.printStackTrace();
		}finally {
			//areaInfoService.findBy("wareId", Integer.parseInt(ids));
			//图片名称
			String name = ids;
			//二维码中包含的信息
			String content = ids;
			Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
			// 指定编码格式
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			// 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
			// 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
			//设置请求头
			response.setHeader("Content-Type", "application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name + ".png", "UTF-8"));

			/* 设置文件ContentType类型，这样设置，会自动判断下载文件类型 *//*
                response.setContentType("multipart/form-data");
                *//* 设置文件头：最后一个参数是设置下载文件名 *//*
                response.setHeader("Content-Disposition", "attachment;filename="+file.getName());*/


			OutputStream outputStream = response.getOutputStream();

			MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);

			outputStream.flush();
			outputStream.close();
		}



	}


	/**
	 * 查询货区设置列表
	 */
	@ApiOperation("zxv")
	@RequiresPermissions("system:areaInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(AreaInfo areaInfo)
	{
		startPage();
        List<AreaInfoDto> list = areaInfoService.selectAreaInfoList(areaInfo);
		return getDataTable(list);
	}

	@PostMapping("/areaInfoPda")
	@ResponseBody
	public TableDataInfo listPda(AreaInfo areaInfo)
	{
		List<AreaInfoDto> list = areaInfoService.selectAreaInfoList(areaInfo);
		return getDataTable(list);
	}
	
	/**
	 * 导出货区设置列表
	 */
	@RequiresPermissions("system:areaInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AreaInfo areaInfo)
    {
    	List<AreaInfoDto> list = areaInfoService.selectAreaInfoList(areaInfo);
        ExcelUtil<AreaInfoDto> util = new ExcelUtil<AreaInfoDto>(AreaInfoDto.class);
        return util.exportExcel(list, "areaInfo");
    }
	
	/**
	 * 新增货区设置
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存货区设置
	 */
	@RequiresPermissions("system:areaInfo:add")
	@Log(title = "货区设置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(AreaInfo areaInfo)
	{

		AreaInfo a1 = areaInfoService.selectAreaInfoByName(areaInfo.getAreaName());
		if(a1!=null){

			return error("货区名已存在!");
		}


		return toAjax(areaInfoService.insertAreaInfo(areaInfo));
	}

	/**
	 * 修改货区设置
	 */
	@GetMapping("/edit/{areaId}")
	public String edit(@PathVariable("areaId") Integer areaId, ModelMap mmap)
	{
		AreaInfo areaInfo = areaInfoService.selectAreaInfoById(areaId);
		mmap.put("areaInfo", areaInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存货区设置
	 */
	@RequiresPermissions("system:areaInfo:edit")
	@Log(title = "货区设置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(AreaInfo areaInfo)
	{

		AreaInfo a1 = areaInfoService.selectAreaInfoByName(areaInfo.getAreaName());
		if(a1!=null){

			return error("货区名已存在!");
		}

		return toAjax(areaInfoService.updateAreaInfo(areaInfo));
	}
	
	/**
	 * 删除货区设置
	 */
	@RequiresPermissions("system:areaInfo:remove")
	@Log(title = "货区设置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(areaInfoService.deleteAreaInfoByIds(ids));
	}
	
}
